import {createRouter, createWebHistory} from "vue-router";
import {isAuthorized} from "@/netForAxios/net.js";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'welcome',
            component: () => import('@/views/WelcomeView.vue'),
            children: [
                {
                    path: '',
                    name: 'welcome-loginPage',
                    component: () => import('@/views/Login/LoginPage.vue'),
                },
                {
                    path: 'register',
                    name: 'welcome-registerPage',
                    component: () => import('@/views/Login/RegisterPage.vue'),

                },
                {
                    path: 'reset',
                    name: 'welcome-resetPage',
                    component: () => import('@/views/Login/RestPage.vue')
                }
            ]
        }, {
            path: '/home',
            name: 'home',
            component: () => import('@/views/Home/Home.vue')
        }
    ]
});
router.beforeEach((to, from, next) => {
    if (to.name.startsWith('welcome-') && !isAuthorized()) {
        next('/home')
    } else if (to.fullPath.startsWith('/home') && isAuthorized()) {
        next('/')
    } else {
        next()
    }
})
export default router;